package com.example.gateway.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;


@Configuration
public class GatewayLoggingConfig {
    private static final Logger logger = LoggerFactory.getLogger(GatewayLoggingConfig.class);

    /**
     * 创建一个全局过滤器，用于记录请求和响应信息
     * @return
     */
    @Bean
    public GlobalFilter loggingFilter() {
        return (exchange, chain) -> {
            logger.info("Request received: {}", exchange.getRequest().getURI());
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                logger.info("Response status: {}", exchange.getResponse().getStatusCode());
            }));
        };
    }

}
